package com.smarthome.activities;

import org.apache.http.HttpResponse;

import com.smarthome.R;
import com.smarthome.implement.*;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class SmartHomeActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		prefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());

		findViews();

		HttpUrlConnection7.Instance().setOnResponseReceivedListener(
				new HttpUrlConnection7.onResponseReceivedListener() {

					@Override
					public void onHttpResponseReceived(HttpResponse httpResponse) {
						// TODO Auto-generated method stub
						onResponseReceived(httpResponse);
					}
				});

		setListeners();
	}

	private final static String REMPASS_STR = "_remPass";
	private final static String AUTOLOGIN_STR = "_autoLogin";

	private String userNameString = null;
	private String userPasswordString = null;
	private boolean rempass;
	private boolean autologin;

	private void setListeners() {
		// TODO Auto-generated method stub
		loginButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getInfoFromViews();

				HttpUrlConnection7.Instance().connectToServer(userNameString,
						userPasswordString, UrlDefines.USER_LOGIN);
				startTV_Controller();
			}
		});

		exitButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	protected void startTV_Controller() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this, TV_Controller.class);
		startActivity(intent);
	}

	protected void getInfoFromViews() {
		// TODO Auto-generated method stub
		HttpUrlConnection7.Instance().SetHostString(
				hostEditText.getText().toString());
		userNameString = userNameEditText.getText().toString();
		userPasswordString = userPasswordEditText.getText().toString();
		rempass = remPassCheckBox.isChecked();
		autologin = autoLoginCheckBox.isChecked();
	}

	protected void savePreference(String keyStr, String valueStr) {
		// TODO Auto-generated method stub
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(keyStr, valueStr);
		editor.commit();
	}

	private SharedPreferences prefs;

	protected void savePreference(String string, boolean bool) {
		// TODO Auto-generated method stub
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(string, bool);
		editor.commit();
	}

	private EditText hostEditText;
	private EditText userNameEditText;
	private EditText userPasswordEditText;
	private CheckBox remPassCheckBox;
	private CheckBox autoLoginCheckBox;
	private Button loginButton;
	private Button exitButton;

	private void findViews() {
		hostEditText = (EditText) findViewById(R.id.EditText_Host);
		userNameEditText = (EditText) findViewById(R.id.EditText_UserName);
		userPasswordEditText = (EditText) findViewById(R.id.EditText_UserPasswd);
		remPassCheckBox = (CheckBox) findViewById(R.id.checkBox_RemPasswd);
		autoLoginCheckBox = (CheckBox) findViewById(R.id.checkBox_AutoLogin);
		loginButton = (Button) findViewById(R.id.Button_Login);
		exitButton = (Button) findViewById(R.id.button_Exit);
	}

	public void onResponseReceived(HttpResponse httpResponse) {
		// TODO Auto-generated method stub
		String resultString = HttpUrlConnection7
				.getStringFromHttpResponce(httpResponse);

		if (resultString == null) {
			Toast.makeText(getApplicationContext(), "Connection Error",
					Toast.LENGTH_LONG).show();
		} else {
			Log.i(this.toString(), resultString);
//			Toast.makeText(getApplicationContext(), resultString,
//					Toast.LENGTH_LONG).show();
			savePreference(userNameString + REMPASS_STR, rempass);
			savePreference(userNameString + AUTOLOGIN_STR, autologin);
			if (rempass) {
				savePreference(userNameString, userPasswordString);
			}
		}
	}
}